package xyz.bali16.module.captcha.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.code.kaptcha.Producer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;


import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.module.captcha.utils.ResultUtils.errorLog;


/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"captcha")
@Api(tags = "验证码接口")
@AllArgsConstructor
public class CaptchaController extends BaseController{
    private static final String KAPTCHA_SESSION_KEY = "KAPTCHA_SESSION_KEY";
    @Autowired
    Producer producer;
    @GetMapping("/capthca.jpg")
    public void kaptcha(HttpServletResponse resp) throws IOException {
        // 验证码
        String text = producer.createText();
        BufferedImage image = producer.createImage(text);
        // 将验证码塞进SESSION_KEY中，实际上redis也可以
        req.getSession().setAttribute(KAPTCHA_SESSION_KEY, text);
        resp.setHeader("Cache-Control", "no-store, no-cache");
        resp.setContentType("image/jpeg");
        ServletOutputStream outputStream = resp.getOutputStream();
        ImageIO.write(image, "jpg", outputStream);

        // 使用方法
        //String capthca = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //System.out.println(capthca);
        //if(vercode == null || !vercode.equalsIgnoreCase(capthca)) {
        //    return Result.failure("验证码输入不正确");
        //}
    }


}
